package crm.model.teacherPC;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeMap;

import crm.model.CRMConst;
import crm.model.teacherPC.bannedList.ApplicationBanned;
import crm.model.teacherPC.bannedList.BrowserBanned;
import crm.model.teacherPC.connectList.ConnectStudent;
import crm.model.teacherPC.net.Aktualisator;
import crm.model.teacherPC.net.NetLauncher;
import crm.model.teacherPC.net.StudentPC;
import crm.model.teacherPC.xml.XMLReader;
import crm.model.teacherPC.xml.XMLWriter;

public class Manager {

	public static final ArrayList<String> WindowList = new ArrayList<String>();

	private final ArrayList<StudentPC> students;
	public static TreeMap<String, ConnectStudent> connectingStudent;
	private final Aktualisator aktualisator;
	private final ArrayList<CRMListener> listener;
	public final BrowserBanned browserbannList;
	public final ApplicationBanned appbannList;
	
	protected XMLWriter studentSave;
	protected XMLReader studentOpen;
	private NetLauncher netLauncher;

	public Manager() {
		listener = new ArrayList<CRMListener>();
		students = new ArrayList<StudentPC>();
		aktualisator = new Aktualisator(students, this);
		connectingStudent = new TreeMap<String, ConnectStudent>();
		browserbannList = new BrowserBanned();
		appbannList = new ApplicationBanned();
		
		studentSave = new XMLWriter(this, browserbannList, appbannList);
		studentOpen = new XMLReader(this, browserbannList, appbannList);
		studentOpen.open();

		new Thread(new Runnable() {

			@Override
			public void run() {
				start();

			}
		}).start();
	}

	public void start() {
		System.out.println("Manager-Start");
		CRMConst.connected = true;
		CRMConst.closed = false;
//		StudentPcFinder.find(this);
		studentSave.save();
		fireConnectionStart();
		netLauncher = new NetLauncher(this);
	}

	public void closeConnection() {
		synchronized (students) {
			CRMConst.closed = true;
			for (StudentPC student : students) {
				student.closeConnection();
			}

			students.clear();
			fireConnectionClosed();
		}
	}

	public void exit() {
		CRMConst.connected = false;
		if (CRMConst.closed) {
			closeConnection();
		}
		aktualisator.thread.interrupt();
		fireCRMExit();
		studentSave.save();
		System.exit(0);
	}

	public void addStudent(StudentPC student) {
		synchronized (student) {
			students.add(student);
		}
	}

	public void removeStudent(StudentPC student) {
		synchronized (student) {
			students.remove(student);
		}
	}

	public Iterator<StudentPC> getStudentIterator() {
		ArrayList<StudentPC> s2 = (ArrayList<StudentPC>)students.clone();
		return s2.iterator();
	}

	public StudentPC getStudent(int i) {
		return students.get(i);
	}

	public int getStudentCount() {
		return students.size();
	}

	public Aktualisator getAktualisator() {
		return aktualisator;
	}

	public void addListener(CRMListener l) {
		listener.add(l);

	}

	public void fireConnectionStart() {
		ArrayList<CRMListener> copyList = (ArrayList<CRMListener>) listener.clone();
		Iterator<CRMListener> iter = copyList.iterator();
		while (iter.hasNext()) {
			CRMListener l = iter.next();
			l.connectionStarted();

		}
	}

	public void fireConnectionClosed() {
		for (CRMListener l : listener) {
			l.connectionClosed();
		}

	}

	public void fireCRMExit() {
		for (CRMListener l : listener) {
			l.exitCRM();
		}
	}

	public void saveStudents() {
		studentSave.save();
	}

	public void fireActulisingListener(StudentPC student) {
		ArrayList<CRMListener> al = (ArrayList<CRMListener>)listener.clone();
		for (CRMListener l : al) {
			l.actualisingComplete(student);
		}
	}

	public void fireStudentRemoved(StudentPC student) {
		for (CRMListener l : listener) {
			l.studentRemoved(student);
		}
	}

	public void fireStudentAdded(StudentPC student) {
		ArrayList<CRMListener> listener = (ArrayList)this.listener.clone();
		for (CRMListener l : listener) {
			l.studentAdded(student);

		}
	}
	public BrowserBanned getBrowserbannList() {
		return browserbannList;
	}

}
